<?php
/**购物车管理*/
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ShopCar extends Model
{
    /***
     * 购物车列表
     * @fit_dt_types 家具规格数据【家具详情id,尺寸数据,详情名称】
     */
    protected static function shopcar_list($param)
    {
        $color_arr = COMMON::color_path();
        //查询字段设置
        $sql_name = 'cart.`cart_id`,cart.`cart_fitment_num`,cart.`cart_status`,cart.`cart_rent_type`,cart.`cart_fitment_id` AS fit_dt_id,
        IF(cart.`cart_rent_type`=1,fit_dt.`details_nimbleprice`,fit_dt.`details_fixprice`) AS price,fit_dt.`details_bond`,
        fit.`fitments_id`,fit.`fitments_name`,fit.`fitments_describe`,
        CONCAT('."'".COMMON::erp_img_path()."'".',img.`fitment_details_imgMaster`) AS img,
         (CASE color.`color_type`
        WHEN 1 THEN '."'".$color_arr['1']['path']."'".' WHEN 2 THEN '."'".$color_arr['2']['path']."'".'                  
        WHEN 3 THEN '."'".$color_arr['3']['path']."'".' WHEN 4 THEN '."'".$color_arr['4']['path']."'".'                  
        WHEN 5 THEN '."'".$color_arr['5']['path']."'".' WHEN 6 THEN '."'".$color_arr['6']['path']."'".'                  
        WHEN 7 THEN '."'".$color_arr['7']['path']."'".' WHEN 8 THEN '."'".$color_arr['8']['path']."'".'                  
         ELSE '."'".$color_arr['9']['path']."'".' END) AS color_type,color.`color_stock_number`,
        (SELECT GROUP_CONCAT(CONCAT(fit_dt_1.`details_id`,'."','".',fit_dt_1.`details_name`,'."','".',fit_dt_1.`details_length`,'."','".',fit_dt_1.`details_width`,'."','".',fit_dt_1.`details_height`)) 
        FROM mzj_fitments AS fit_1 LEFT JOIN mzj_fitment_details AS fit_dt_1 ON fit_dt_1.`details_pid`=fit_1.`fitments_id` WHERE fit_1.`fitments_id`=fit.`fitments_id` AND fit_1.`fitments_isdel`=1 AND fit_dt_1.`details_isdel`=1) AS fit_dt_types,
        CONCAT(fit_dt.`details_length`,'."'*'".',fit_dt.`details_width`,'."'*'".',fit_dt.`details_height`) AS color_size';

        $res = DB::select('SELECT '.$sql_name.' FROM mzj_cart AS cart
        LEFT JOIN mzj_fitment_details AS fit_dt ON fit_dt.`details_id`=cart.`cart_fitment_id`
        LEFT JOIN mzj_fitments AS fit ON fit.`fitments_id`=fit_dt.`details_pid`
        LEFT JOIN mzj_details_img AS img ON img.`fitment_details_id`=fit_dt.`details_id`
        LEFT JOIN mzj_fitment_color AS color ON color.`color_details_id`=fit_dt.`details_id`
        WHERE fit.`fitments_isdel`=1 AND fit_dt.`details_isdel`=1 AND cart.`cart_user_id`='.$param['id']);
        return $res;
    }

    /***购物车添加*/
    protected static function shopcar_add($param)
    {
        $cart = DB::table('cart')
            ->where('cart_user_id',$param['cart_user_id'])
            ->where('cart_fitment_id',$param['cart_fitment_id'])
            ->where('cart_rent_type',$param['cart_rent_type'])
            ->first();
        if(count($cart)>0){
            $res = DB::table('cart')->where('cart_id',$cart->cart_id)->increment('cart_fitment_num',$param['cart_fitment_num']);
        } else {
            $res = DB::table('cart')->insertGetId($param);
        }
        return $res;
    }

    /**购物车删除*/
    protected static function shopcart_destroy($param)
    {
        if(is_array($param)){ //批量删除操作
            $res=DB::table('cart')->whereIn('cart_id',$param)->delete();
        }else{
            $res=DB::table('cart')->where('cart_id',$param)->delete();
        }
        return $res;
    }

    /**
     * 购物车编辑
     * @param 编辑数据参数
     */
    protected static function shopcar_edit($param)
    {
        $data=array();
        $id=$param['id'];
        unset($param['id']);
        foreach ($param as $key=>$value){
            $data[$key]=$value;
        }
        $res = DB::table('cart')->where('cart_id',$id)->update($data);
        return $res;
    }

    /**购物车结算*/
    protected static function shopcar_finish()
    {
        //
    }

}
